package com.longxi.tianjintianbao.ui.presenter.complaint;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.BaseResponse;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.model.Personnel;
import com.longxi.tianjintianbao.model.complaint.Complaint;
import com.longxi.tianjintianbao.model.complaint.ComplaintFollow;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.dialog_fragment.ComplaintDealDialogFragment;
import com.longxi.tianjintianbao.ui.view.complaint.ISaveComplaintAtView;
import com.longxi.tianjintianbao.utils.LogUtils;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.widget.AddComplaintFollowDialog;
import com.longxi.tianjintianbao.widget.UpdateComplaintDialog;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 70339 on 2018/5/3.
 * 代码无八阿哥
 * 阿弥陀佛
 */
public class SaveComplaintAtPresenter extends BasePresenter<ISaveComplaintAtView> {

    private String type = "0";

    public SaveComplaintAtPresenter(BaseActivity context) {
        super(context);
    }

    public void setBar() {
        mContext.setTitle("投诉处理");
        mContext.setTvLeft(MyUtils.getString(R.string.back));
    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != mContext.RESULT_OK){
            return;
        }
        switch (requestCode) {
            case PictureConfig.CHOOSE_REQUEST:
                // 图片选择结果回调
                addPicUtils.selectList = PictureSelector.obtainMultipleResult(data);
                // 例如 LocalMedia 里面返回三种path
                // 1.media.getPath(); 为原图path
                // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                // 如果裁剪并压缩了，已取压缩路径为准，因为是先裁剪后压缩的
                for (LocalMedia media : addPicUtils.selectList) {
                    LogUtils.i("图片-----》", media.getPath());
                }
                gridImageAdapter.setList(addPicUtils.selectList);
                gridImageAdapter.notifyDataSetChanged();
                break;
        }
    }

    public void acceptTask(){
        String content = getView().getEtContent().getText().toString().trim();
        if("".equals(content)){
            MyUtils.showToast("请填写处理结果");
            return;
        }

        if(addPicUtils.selectList.size() == 0){
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("提示");
            builder.setMessage("当前投诉处理未选择图片是否需要上传图片");
            builder.setPositiveButton("需要", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });
            builder.setNegativeButton("不需要", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    saveComplaint(type, content);
                }
            });
            builder.show();
        }else{
            saveComplaint(type, content);
        }
    }

    public void getDealType(int i){
        switch (i) {
            case R.id.rBtnPhone:
                type = "0";
                break;
            case R.id.rBtnComeOn:
                type = "1";
                break;
        }
    }

    public void saveComplaint(String type, String result){
        mContext.showWaitingDialog(MyUtils.getString(R.string.upload));

        String image1 = "";
        String image2 = "";
        for (int i = 0; i < addPicUtils.selectList.size(); i++) {
            if(i == 0){
                image1 = MyUtils.imageToBase64(addPicUtils.selectList.get(i).getCompressPath());
            }else if(i == 1){
                image2 = MyUtils.imageToBase64(addPicUtils.selectList.get(i).getCompressPath());
            }
        }

        JSONObject object = new JSONObject();
        try {
            object.put("billid", mContext.getIntent().getStringExtra(AppConst.BILLID));
            object.put("userid", MyApplication.loginUser.userid);
//            object.put("handletype", type);
            object.put("result", result);
            object.put("image1", image1);
            object.put("image2", image2);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().saveComplaint(object)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new MySubscriber<BaseResponse>(mContext) {
                @Override
                public void onError(ExceptionHandle.ResponeThrowable throwable) {
                    mContext.hideWaitingDialog();
                    MyUtils.showToast(throwable.message);
                }

                @Override
                public void onNext(BaseResponse result) {
                    mContext.hideWaitingDialog();
                    mContext.setResult(mContext.RESULT_OK);
                    MyUtils.showToast(result.getMsg());
                    mContext.finish();
                }
            });
    }

}
